// pages/partyFee/partyFee.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPay: false,
    title: '请选择活动付款类型',
    fee: {
      aa: false,
      allin: false,
      free: false,
      perCapita: '',
      payer: '',
      chooseConsume: 1
    }
  },
  getPayStatus() {
    var that = this;
    app.req({
      url: 'wechat/dictionary/listBydictType',
      data: {
        dictType: 'PAY_ON_OFF'
      },
      method: 'POST',
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            isPay: res.data[0].dictValue == 1
          })
          if (res.data[0].dictValue == 0){
            that.toggle('fee.allin', true);
            that.toggle('fee.aa', false);
            that.toggle('fee.chooseConsume', 2);
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  save() {
    this.setData({
      'fee.payer': this.data.fee.payer.trim()
    });
    if (this.data.fee.aa && this.data.fee.perCapita == '') {
      wx.showToast({
        title: '请输入人均费用',
        icon: 'none',
        duration: 2000
      })
    } else if (this.data.fee.aa && !(/^[0-9]+.?[0-9]*$/.test(this.data.fee.perCapita))){
      wx.showToast({
        title: '请输入正确的数字',
        icon: 'none',
        duration: 2000
      })
    } else if (this.data.fee.aa && this.data.fee.perCapita >= 1000000) {
      wx.showToast({
        title: '请填写1000000以内的数字',
        icon: 'none',
        duration: 2000
      })
    }else if (this.data.fee.allin && this.data.fee.payer == '') {
      wx.showToast({
        title: '请输入请客人姓名',
        icon: 'none',
        duration: 2000
      })
    } else if (this.data.fee.payer.length > 10) {
      wx.showToast({
        title: '请客人姓名最多输入10个字符',
        icon: 'none',
        duration: 2000
      })
    } else {
      let pages = getCurrentPages(),
        prevPage = pages[pages.length - 2];
      var perCapita = this.data.fee.perCapita,
        payer = this.data.fee.payer;
      if (this.data.fee.aa) {
        prevPage.toggle('act.chooseConsume', 1);
        prevPage.toggle('act.perCapita', perCapita);
        prevPage.toggle('act.payer', '');
      } else if (this.data.fee.allin) {
        prevPage.toggle('act.chooseConsume', 2);
        prevPage.toggle('act.perCapita', '');
        prevPage.toggle('act.payer', payer);
      } else if (this.data.fee.free) {
        prevPage.toggle('act.chooseConsume', 3);
        prevPage.toggle('act.perCapita', '');
        prevPage.toggle('act.payer', '');
      }
      wx.navigateBack({
        delta: 1
      })
    }
  },
  tabAA() {
    let chose = !this.data.fee.aa;
    if (chose) {
      this.toggle('fee.aa', chose);
      this.toggle('fee.allin', !chose);
      this.toggle('fee.free', !chose);
    }
  },
  tabAllin() {
    let chose = !this.data.fee.allin;
    if (chose) {
      this.toggle('fee.allin', chose);
      this.toggle('fee.aa', !chose);
      this.toggle('fee.free', !chose);
    }
  },
  tabFree() {
    let chose = !this.data.fee.free;
    if (chose) {
      this.toggle('fee.free', chose);
      this.toggle('fee.allin', !chose);
      this.toggle('fee.aa', !chose);
    }
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  aaInput(e) {
    var a = e.detail.value;
    if (a.toString().split(".").length > 2) {
      this.toggle('fee.perCapita', this.data.fee.perCapita);
    }else if (a.toString().split(".").length < 2) {
      this.toggle('fee.perCapita', e.detail.value);
    } else if (a.toString().split(".")[1].length > 2) {
      this.toggle('fee.perCapita', this.data.fee.perCapita);
    } else {
      this.toggle('fee.perCapita', e.detail.value);
    }
  },
  allinInput(e) {
    this.toggle('fee.payer', e.detail.value);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.chooseConsume) {
      this.setData({
        fee: {
          chooseConsume: options.chooseConsume,
          aa: options.chooseConsume == 1,
          allin: options.chooseConsume == 2,
          free: options.chooseConsume == 3,
          perCapita: options.perCapita || '',
          payer: options.payer || ''
        }
      })
    }
    this.getPayStatus();
  },
  //获取列表值
  getItemList(){
    var that = this;
    app.req({
      url: 'wechat/dictionary/listBydictType',
      data: {
        dictType: 'CHOOSE_CONSUME'
      },
      method: 'POST',
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            tranList: res.data
          })
          if (options.transportation && options.transportation != '') {
            let transportation = ',' + options.transportation + ',',
              tranList = res.data;
            for (let item of tranList) {
              item.isChoose = transportation.indexOf(',' + item.dictValue + ',') >= 0;
            }
            that.setData({
              transportation: options.transportation,
              tranList: tranList,
              tranFlag: true
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

})